//
//  MXChartBase.m
//  MXArcChart
//
//  Created by 马彦 on 16/11/4.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXChartBase.h"

#define Outer_Arc 1.5  //外弧
#define Inner_Arc 0.5  //内弧
#define TEXT_FONT 12 //字体大小

@interface MXChartBase ()

@property (nonatomic, assign) CGFloat LineWidth;

@end

@implementation MXChartBase

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.sliceSpace = 1.0;
    }
    return self;
}

-(void)drawRect:(CGRect)rect {
    [self setFrame:rect];
}


/**
 *  圆弧
 *
 *  @param center     中心点
 *  @param radius     半径
 *  @param startAngle 开始角度
 *  @param endAngle   结束角度
 *  @param color      颜色
 */
- (void)chartBaseARCWithcenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [color set];
    [path addArcWithCenter:center radius:radius startAngle:startAngle  endAngle:endAngle clockwise:YES];
    path.lineWidth = radius;
    [path stroke];
    
    
}

/**
 *  绘制方块
 *
 *  @param rect  位置大小
 *  @param color 颜色
 *  @param fill  是否填充
 */
- (void)chartBaseSquareWithRect:(CGRect)rect color:(UIColor *)color fill:(BOOL)fill {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    [color set];
    path.lineWidth = 1;
    if (fill) {
        [path fill];
    } else {
        [path stroke];
    }
}

/**
 *  圆弧之间的间距
 *
 *  @param center     中心点
 *  @param radius     半径
 *  @param startAngle 开始角度
 *  @param endAngle   结束角度
 *  @param color      颜色
 */
- (void)chartBaseWithcenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGFloat StartRadian = startAngle;
    CGFloat StartSpacing = M_PI/ 180 * self.sliceSpace + startAngle;
    CGFloat StartEndSpacing = startAngle + (M_PI / 180 * self.sliceSpace) / 3;
    CGFloat x1 = center.x + radius * Outer_Arc * cos(StartRadian);
    CGFloat y1 = center.y + radius * Outer_Arc * sin(StartRadian);
    CGFloat x2 = center.x + radius * Outer_Arc * cos(StartEndSpacing);
    CGFloat y2 = center.y + radius * Outer_Arc * sin(StartEndSpacing);
    
    CGFloat x3 = center.x + radius * Inner_Arc * cos(StartSpacing);
    CGFloat y3 = center.y + radius * Inner_Arc * sin(StartSpacing);
    CGFloat x4 = center.x + radius * Inner_Arc * cos(StartRadian);
    CGFloat y4 = center.y + radius * Inner_Arc * sin(StartRadian);

    [path moveToPoint:CGPointMake(x1, y1)];
    [path addLineToPoint:CGPointMake(x2, y2)];
    [path addLineToPoint:CGPointMake(x3, y3)];
    [path addLineToPoint:CGPointMake(x4, y4)];
 
    CGFloat endRadian = endAngle;
    CGFloat endSpacing = endRadian - M_PI/ 180 * self.sliceSpace;
    CGFloat endEndSpacing = endRadian - (M_PI / 180 * self.sliceSpace) / 3;
    
    CGFloat x5 = center.x + radius * Outer_Arc * cos(endRadian);
    CGFloat y5 = center.y + radius * Outer_Arc * sin(endRadian);
    CGFloat x6 = center.x + radius * Outer_Arc * cos(endEndSpacing);
    CGFloat y6 = center.y + radius * Outer_Arc * sin(endEndSpacing);
    
    CGFloat x7 = center.x + radius * Inner_Arc * cos(endSpacing);
    CGFloat y7 = center.y + radius * Inner_Arc * sin(endSpacing);
    CGFloat x8 = center.x + radius * Inner_Arc * cos(endRadian);
    CGFloat y8 = center.y + radius * Inner_Arc * sin(endRadian);
    
    [path moveToPoint:CGPointMake(x5, y5)];
    [path addLineToPoint:CGPointMake(x6, y6)];
    [path addLineToPoint:CGPointMake(x7, y7)];
    [path addLineToPoint:CGPointMake(x8, y8)];
    

    [self.backgroundColor set];
    path.lineWidth = 0.5;
    
    [path fill];
}

/**
 *  圆弧外面的指示线
 *
 *  @param text       只是线上数值
 *  @param center     中心点
 *  @param radius     半径
 *  @param startAngle 开始角度
 *  @param endAngle   结束角度
 *  @param color      颜色
 */
- (void)drawLineWithtext:(NSString *)text center:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color {
    
    UIColor *textColor = nil;
    if (!self.textColor) {
        textColor = color;
    } else {
        textColor = self.textColor;
    }
    if (self.width < 200 || self.height < 200) {
        self.textFout = TEXT_FONT * 0.6;
    } else {
        self.textFout = TEXT_FONT;
    }
    
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:self.textFout],NSForegroundColorAttributeName:textColor};
    
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    CGFloat StartRadian = startAngle;
    CGFloat midSpace = StartRadian + (endAngle - startAngle) * 0.5;
    
    CGFloat x1 = center.x + radius * (Outer_Arc + 0.1) * cos(midSpace);
    CGFloat y1 = center.y + radius * (Outer_Arc + 0.1) * sin(midSpace);
    CGFloat x2 = center.x + radius * (Outer_Arc + 0.3) * cos(midSpace);
    CGFloat y2 = center.y + radius * (Outer_Arc + 0.3) * sin(midSpace);
    
    CGFloat x3 = 0;
    CGFloat y3 = y2;
    if (cos(midSpace) <= 0) {
        x3 = center.x - radius * (Outer_Arc + 2.0) * (0.68);
        [self drawText:text point:CGPointMake(x3, y3 - size.height) color:color Attributes:dict];
    } else {
        x3 = center.x + radius * (Outer_Arc + 2.0) * 0.68;
        [self drawText:text point:CGPointMake(x3 - size.width, y3 - size.height) color:color Attributes:dict];
    }
    
    [path moveToPoint:CGPointMake(x1, y1)];
    [path addLineToPoint:CGPointMake(x2, y2)];
    [path addLineToPoint:CGPointMake(x3, y3)];
    [color set];
    path.lineWidth = 1;

    [path stroke];
    
    [self drawLineRoundWithcenter:CGPointMake(x1, y1) radius:3 startAngle:0 endAngle:M_PI * 2 color:color fill:YES];
    
}

/**
 *  指示线上的圆
 *
 *  @param center     中心点
 *  @param radius     半径
 *  @param startAngle 开始角度
 *  @param endAngle   结束角度
 *  @param color      颜色
 *  @param fill       是佛填充
 */
- (void)drawLineRoundWithcenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color fill:(BOOL)fill{
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    
    [color setFill];
    path.lineWidth = 1;
    
    if (fill) {
        [path fill];
    } else {
        [path stroke];
    }
    
}

/**
 *  文字
 *
 *  @param text  文字
 *  @param rect  开始点
 *  @param color 颜色
 *  @param dict  文字信息
 */
- (void)drawText:(NSString *)text point:(CGPoint )rect color:(UIColor *)color Attributes:(NSDictionary <NSString *,id> *)dict {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [[NSString stringWithFormat:@"%@",text] drawAtPoint:rect withAttributes:dict];
    [color set];
    CGContextDrawPath(context, kCGPathFill);
}

- (void)setTextColor:(UIColor *)textColor {
    
    if (!textColor) {
        _textColor = [UIColor blackColor];
    } else {
        _textColor = textColor;
    }
}

@end
